﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlInventoryProcess
    {
        /// <summary>
        /// 5.0 原料库存查询 Add By Quinn.Ling 201703
        /// DataSource:Inventory
        /// </summary>

        public static string GetPDAInventoryData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string qty = string.Empty; //良品数量
            string allocated_qty = string.Empty;
            string picked_qty = string.Empty;
            string backup_qty = string.Empty; //备货数量
            string check_qty = string.Empty; //待检数量
            string putaway_qty = string.Empty; //待上架数量 
            string rejects_qty = string.Empty; //不良品数量
            string serial = string.Empty;
            string return_status = "OK";
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                //App.ws = new Fcl.WSOP.sqldbop();
                //App.ws.Init(System.Configuration.ConfigurationManager.ConnectionStrings["adpwmsConnectionString"].ConnectionString);
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("material");

                try
                {
                    userid = nodeSku.SelectSingleNode("userid").InnerText;
                    whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                    master_label = nodeSku.SelectSingleNode("master_label").InnerText;
                    try
                    {
                        string[] s = master_label.Split(new char[] { '|' });
                        sku = s[0].ToString();
                    }
                    catch( Exception ex)
                    { 
                    App.Log("PDAXmlInventoryProcess.cs-2", ex.Message.ToString());
                    }


                    #region 数据源获取
                    //良品数量 获取
                    strSql = "select isnull(sum(qty)-sum(picked_qty)-sum(allocated_qty),0) as qty,isnull(sum(picked_qty),0) as backup_qty ";
                    strSql += "from Inventory where sku='" + sku + "' and status='OK'";
                    DataTable dt = App.GetDataTable(App.DBconWMS,strSql,"Inventory");
                    if (dt.Rows.Count > 0)
                    {
                        qty = dt.Rows[0]["qty"].ToString();
                    }
                    else
                    {
                        return_status = "良品数据获取失败";
                    }
                    //备货数量 获取
                    strSql = "select isnull(sum(picked_qty),0) as backup_qty  from Inventory where sku='" + sku + "' and status='OK'";
                    DataTable dtbackup = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dtbackup.Rows.Count > 0)
                    {
                        backup_qty = dtbackup.Rows[0]["backup_qty"].ToString();
                    }
                    else
                    {
                        return_status = "备货数据获取失败";
                    }

                    //待检数量 获取
                    strSql = "  select isnull(sum(qty),0) as check_qty from Inventory where sku='" + sku + "' and status='HOLD'";
                    DataTable dtcheck = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dtcheck.Rows.Count > 0)
                    {
                        check_qty = dtcheck.Rows[0]["check_qty"].ToString();
                    }
                    else
                    {
                        return_status = "待检库存数据获取失败";
                    }

                    //待上架数量 获取
                    strSql = "select isnull(sum(act_qty),0) as putaway_qty from ReceiptDetail ReceiptDetail where sku='" + sku + "' and (status='03' or status='02')    and pre_qty!=0";
                    DataTable dtaway = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dtaway.Rows.Count > 0)
                    {
                        putaway_qty = dtaway.Rows[0]["putaway_qty"].ToString();
                    }
                    else
                    {
                        return_status = "待上架库存数据获取失败";
                    }

                    //不良品数量 获取
                    strSql = "select isnull(sum(qty),0) as rejects_qty from Inventory where sku='" + sku + "' and status='MRB'";
                    DataTable dtrejects = App.GetDataTable(App.DBconWMS, strSql, "Inventory");
                    if (dtrejects.Rows.Count > 0)
                    {
                        rejects_qty = dtrejects.Rows[0]["rejects_qty"].ToString();
                    }
                    else
                    {
                        return_status = "不良品库存数据获取失败";
                    }

                    #endregion

                }
                catch( Exception ex) {
                    App.Log("PDAXmlInventoryProcess.cs-3", ex.Message.ToString());
                }
                #endregion

                #region 构建XML报文
                XmlNode order_result = docreturn.CreateElement("order_return");

                XmlElement useridResult = docreturn.CreateElement("userid");
                useridResult.InnerText = userid;
                order_result.AppendChild(useridResult);

                XmlElement whseidResult = docreturn.CreateElement("whseid");
                whseidResult.InnerText = whseid;
                order_result.AppendChild(whseidResult);

                XmlElement master_labelResult = docreturn.CreateElement("master_label");
                master_labelResult.InnerText = master_label;
                order_result.AppendChild(master_labelResult);

                XmlElement skuResult = docreturn.CreateElement("sku");
                skuResult.InnerText = sku;
                order_result.AppendChild(skuResult);

                XmlElement qtyResult = docreturn.CreateElement("qty");
                qtyResult.InnerText = qty;
                order_result.AppendChild(qtyResult);

                XmlElement check_qtyResult = docreturn.CreateElement("check_qty");
                check_qtyResult.InnerText = check_qty;
                order_result.AppendChild(check_qtyResult);

                XmlElement putaway_qtyResult = docreturn.CreateElement("putaway_qty");
                putaway_qtyResult.InnerText = putaway_qty;
                order_result.AppendChild(putaway_qtyResult);

                XmlElement backup_qtyResult = docreturn.CreateElement("backup_qty");
                backup_qtyResult.InnerText = backup_qty;
                order_result.AppendChild(backup_qtyResult);

                //if (type.Trim() == "成品库存查询")
                //{
                //    XmlElement picked_qtyResult = docreturn.CreateElement("picking_qty");
                //    picked_qtyResult.InnerText = picked_qty;
                //    order_result.AppendChild(picked_qtyResult);
                //}

                XmlElement rejects_qtyResult = docreturn.CreateElement("rejects_qty");
                rejects_qtyResult.InnerText = rejects_qty;
                order_result.AppendChild(rejects_qtyResult);

                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                return_statusResult.InnerText = return_status;
                order_result.AppendChild(return_statusResult);

                rootResult.AppendChild(order_result);
                #endregion
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlInventoryProcess.cs-1", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlInventoryProcess:GetPDAInventoryData;" + "  " + ex.Message);
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }



    }
}
